from django.contrib import admin

# 1. 导入默认UserAdmin 作为Base Class
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from .models import NeedAuthenticatedSchoolMateProfile, SchoolMateProfile


# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class NeedAuthenticatedSchoolMateProfileInline(admin.StackedInline):
    model = NeedAuthenticatedSchoolMateProfile
    can_delete = False
    verbose_name_plural = '用户信息'


# 2.  Define a new User admin
class UserAdmin(BaseUserAdmin):
    # 3. 重新定义 list_display
    list_display = ('username', 'email', 'is_active', 'date_joined', 'is_staff')
    inlines = (NeedAuthenticatedSchoolMateProfileInline,)


class NeedAuthenticatedSchoolMateProfileAdmin(admin.ModelAdmin):
    '''设置列表可显示的字段'''
    list_display = (
        'student_id', 'name', 'mobile', 'authentication_status', 'attendance_date', 'institute_name', 'major',
        'class_name',)
    '''设置过滤选项'''
    list_filter = ('authentication_status', 'institute_name', 'major', 'class_name')
    '''每页显示条目数'''
    list_per_page = 10
    '''设置可编辑字段'''
    list_editable = ('authentication_status',)
    '''按日期月份筛选'''
    # date_hierarchy = 'attendance_date'
    '''按发布日期排序'''
    ordering = ('student_id',)
    search_fields = ('name', 'student_id', 'mobile')


class SchoolMateProfileAdmin(admin.ModelAdmin):
    '''设置列表可显示的字段'''
    list_display = (
        'student_id', 'name', 'mobile', 'attendance_date', 'institute_name', 'major',
        'class_name',)
    '''设置过滤选项'''
    list_filter = ('institute_name', 'major', 'class_name')
    '''每页显示条目数'''
    list_per_page = 10
    '''按发布日期排序'''
    ordering = ('student_id',)

    search_fields = ('name', 'student_id', 'mobile')


# 4. 注销 User
admin.site.unregister(User)
# 5. 重新注册 User
admin.site.register(User, UserAdmin)
admin.site.register(NeedAuthenticatedSchoolMateProfile, NeedAuthenticatedSchoolMateProfileAdmin)
admin.site.register(SchoolMateProfile, SchoolMateProfileAdmin)

# 修改网页title和站点header。
admin.site.site_title = "校友会后台管理系统"
admin.site.site_header = "校友会后台管理系统"
